import dayjs from "dayjs";

export const formatTime = (time: number, format: string) => {
  const date = dayjs(time);
  return date.format(format);
};

export const keepTwoDecimal = (num, n = 2) => {
  let f = parseFloat(num);
  if (isNaN(f)) {
    return false;
  }
  f = Math.round(num * Math.pow(10, n)) / Math.pow(10, n); // n幂   
  let s = f.toString();
  let rs = s.indexOf('.');
  //判定如果是整数，增加小数点再补0
  if (rs < 0) {
    rs = s.length;
    s += '.';
  }
  while (s.length <= rs + n) {
    s += '0';
  }
  console.log(s.slice(-1), s.slice(s.length - 1))
  s = s.slice(-1) == '.' ? s.slice(0, s.length - 1) : s
  return s;
}

export const validatePhoneNumber = (phone) => {
  const regex = /^(?:(?:\+|00)86)?1(?:3\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\d|9[0-35-9])\d{8}$/;
  return regex.test(phone);
}

export const validateLicensePlate = (plateNumber) => {
  // 普通燃油车车牌正则表达式
  const regularPlateRegex = /^[京津沪渝冀晋辽吉黑苏浙皖闽赣鲁豫鄂湘粤琼川黔滇陕甘青藏港澳台桂宁新]{1}[A-Z]{1}[A-Z0-9]+$/;

  // 新能源车牌正则表达式
  // const newEnergyPlateRegex = /^[京津沪渝冀晋辽吉黑苏浙皖闽赣鲁豫鄂湘粤琼川黔滇陕甘青藏港澳台桂宁新]{1}[A - HJ - NP - Z]{1}(([0-9]{5}[DF])|([DF][A - HJ - NP - Z0 - 9]{4}))$/;

  // 先检查是否为普通燃油车车牌
  if (regularPlateRegex.test(plateNumber)) {
    return true;
  }

  // // 再检查是否为新能源车牌
  // if (newEnergyPlateRegex.test(plateNumber)) {
  //   return true;
  // }

  return false;
}


export const validatePartLicensePlate = (input) => {
  const regex = /^[a-zA-Z0-9]{3,4}$/;
  return regex.test(input);
}